<?php

namespace App\Listeners;

use App\Events\FlowChangeEvent;
use App\Events\PositionChangeEvent;
use App\Models\Car;
use App\Models\User;
use App\Services\CarService;
use App\Services\PublishTemplateService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class PushTemplateListener implements ShouldQueue
{

    public $delay = 5;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        if ($this->get_curr_time_section())  {
            $this->delay = 15;
        } else {
            $this->delay = 5;
        }
    }

    function get_curr_time_section()
    {
        $checkDayStr = date('Y-m-d ',time());
        $timeBegin1 = strtotime($checkDayStr."07:00".":00");
        $timeEnd1 = strtotime($checkDayStr."07:00".":30");
        $curr_time = time();
        if($curr_time >= $timeBegin1 && $curr_time <= $timeEnd1)
        {
            return true;
        }
        return false;
    }




    public function handle(FlowChangeEvent $event)
    {
        $car_id = $event->car_id;
        $car = Car::find($car_id);
        $template = new PublishTemplateService();
//        $arr_openid = CarService::getOpenIds($car->driver, $car->supercargo);
        $user_id = $car->user_id;
//        if (!$user_id)
//            return;


        $user = User::find($user_id);
        $status = $event->status;
        if ($status == 1) {
            $front_total = CarService::getFrontQueueTotal($car);
            if ($user && $user->weixin_openid) {
                $template->appoint($user->weixin_openid, $car->order_sn, $front_total);
            }
        } else if($status == 2) {
            if ($user && $user->weixin_openid)
                $template->signIn($user->weixin_openid, $user->title);
        }
        return;

        if ($car->status == 1) {
            $front_total = CarService::getFrontQueueTotal($car->id);
//            foreach ($arr_openid as $openid => $title) {
                if ($user && $user->weixin_openid) {
                    $template->appoint($user->weixin_openid, $car->order_sn, $front_total);
                }
//            }
//            CarService::signInNotify();
            //判断里面是否有可进场
//            if (CarService::canSignIn()) {
//                //提醒最新一个进场
//                $car_one = CarService::getWaitSignInOpenId();
//                if ($car_one) {
//                    if (!$car_one->user_id)
//                        return;
//                    $user_2 = User::find($car_one->user_id);
//                    if (!$user_2)
//                        return;
////                    foreach ($result['openIds'] as $_openid => $_title) {
//                        $template->callSignIn($user_2->weixin_openid, $car_one->car_num, $car_one->order_sn);
////                    }
//                }
//            }
        } else if($car->status == 2) {
//            foreach ($arr_openid as $openid => $title) {
            if ($user && $user->weixin_openid)
                $template->signIn($user->weixin_openid, $user->title);
//            }
        } else if ($car->status == 3) {
//            foreach ($arr_openid as $title => $openid) {
            if ($user && $user->weixin_openid) {
                $template->signOut($user->weixin_openid, $user->title);
            }

//            }
            CarService::signInNotify();
            // 提醒最新一个进场
//            $result = CarService::getWaitSignInOpenId();
//            if (!empty($result)) {
//                foreach ($result['openIds'] as $_openid => $_title ) {
//                    $template->callSignIn($_openid, $result['car_num'], $result['order_sn']);
//                }
//            }

            //提醒最新一个进场
//            $car_one = CarService::getWaitSignInOpenId();
//            if ($car_one) {
//                if (!$car_one->user_id)
//                    return;
//                $user_2 = User::find($car_one->user_id);
//                if (!$user_2)
//                    return;
////                    foreach ($result['openIds'] as $_openid => $_title) {
//                $template->callSignIn($user_2->weixin_openid, $car_one->car_num, $car_one->order_sn);
////                    }
//            }

        }

    }
}
